/**
 * 
 */
package com.user.mng;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import com.data.mng.Dao;

/**
 * @author Administrator
 *
 */
public class User {
	private int id;
	private String name="";
	private String password="";
	private static Dao dao=new Dao();
	
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", password=" + password
				+ "]";
	}

	public int addUser()throws SQLException{
		int ret=0;
		String sql="insert into username values('"+name+"','"+password+"')";
		ret=dao.executeUpdate(sql);
		return ret;
	}
	
	public static int addUser(String sName,String sPassword)throws SQLException{
		int ret=0;
		String sql="insert into username values('"+sName+"','"+sPassword+"')";
		ret=dao.executeUpdate(sql);
		return ret;
	}
	
	public int delUser()throws SQLException{
		int ret=0;
		String sql="delete from username where sName='"+name+"'";
		ret=dao.executeUpdate(sql);
		return ret;
	}
	
	public static int delUser(String sName,String sPassword)throws SQLException{
		int ret=0;
		String sql="delete from username where sName='"+sName+"'";
		ret=dao.executeUpdate(sql);
		return ret;
	}
	
	public static User getUser(String sName,String sPassword)throws SQLException{
		User ret=null;
		List<HashMap<String,Object>> selected=null;
		String sql="select name,password from username where sName='"+sName+"'";
		selected=dao.executeQuery(sql);
		if(selected.size()==1){
			ret=new User();
			ret.name=selected.get(0).get("name").toString();
			ret.password=selected.get(0).get("password").toString();
		}
		return ret;
	}
	
	public  User getUser()throws SQLException{
		User ret=null;
		List<HashMap<String,Object>> selected=null;
		String sql="select name,password from username where sName='"+name+"'";
		selected=dao.executeQuery(sql);
		if(selected.size()==1){
			ret=new User();
			ret.name=selected.get(0).get("name").toString();
			ret.password=selected.get(0).get("password").toString();
		}

		return ret;
	}
	
	public int updUser(String sName,String oldPassword,String newPassword)throws SQLException{
		//int ret=0;
		//String sql="insert into username values('"+sName+"','"+oldPassword+"')";
		//ret=dao.executeUpdate(sql);
		//return ret;
		throw new SQLException();
	}
}
